<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/5/6
 * Time: 10:51
 */

namespace app\controller\admin;
use app\model\Standard AS model;

class Standard extends Base
{
    public function list(){
        $where = [];
        $post = input('post.');
        !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
        $list = model::where($where)
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->view('/admin/standard/list',['list' => $list]);
    }
    public function info($id = 0){
        if (!empty($id))
            $info = model::find($id)->toArray();
        else
            $info = [
                'standard' => '基础算法带台阶',
                'type_name' => '平焊法兰',
//                (((R1/2)**2-((R2-C1*2)/2)**2-(KR/2)**2*KM)*H1-(((TWR-C1*2)/2)**2-(R2-C1*2)**2)))*TH*PI/1000000*MD
                'algorithm' => '(((R1/2)**2-((R2-C1*2)/2)**2-(KR/2)**2*KM)*H1-((((TWR-C1*2)/2)**2-(R2-C1*2)**2))*TH)*PI/1000000*MD',
                'blank_algorithm' => '(((R1/2)**2-((R2-C1*2)/2)**2)*PI/1000000*MD',
                'variable' => [
                    ['name' => '密度','symbol' => 'MD'],
                    ['name' => '外径','symbol' => 'R1'],
                    ['name' => '接管外径','symbol' => 'R2'],
                    ['name' => '接管壁厚','symbol' => 'C1'],
                    ['name' => '片厚(不含台阶)','symbol' => 'H1'],
                    ["name" => "螺栓孔数", "symbol" => "KM"],
                    ["name" => "螺栓孔径", "symbol" => "KR"],
                    ["name" => "台阶高度", "symbol" => "TH"],
                    ["name" => "台阶外径", "symbol" => "TWR"]
                ],
                'other_algorithm' => [
                    [
                        'id' => 1,
                        'name' => "锻打下料",
                        "vara" => "DW1",
                        "algorithm" => "weight+W1+L1"
                    ],
                    [
                        "id" => 1,
                        "name" => "锻打毛坯价格",
                        "vara" => "DP1",
                        "algorithm" => "weight*(MPO+MPT)/1000+W1*P1/1000-W2*RP/1000"
                    ]
                ],
                'other_variable' => [
                    ["id" => 1, "name" => "锻打损耗", "type" => 1, "vara" => "L1"],
//                    ["id" => 1, "name" => "锻打毛坯重量", "type" => 1, "vara" => "W1"],
                    ["id" => 1, "name" => "锻件冲子重量", "type" => 1, "vara" => "W1"],
//                    ["id" => 1, "name" => "材料选择", "type" => 2, "vara" => "MP1"],
//                    ["id" => 1, "name" => "加工费", "type" => 3, "vara" => "MP2"],
//                    ["id" => 1, "name" => "材料选择", "type" => 2, "vara" => "CHAILIAOHUILUFEIYONG"],
                    ["id" => 1, "name" => "加工费", "type" => 3, "vara" => "P1"]
                ],
                'other_standard' => [["id" => 1, "name" => "锻打毛胚"]]
        ];
        $info['variable'] = json_decode(json_encode($info['variable']),true);
        $info['other_algorithm'] = json_decode(json_encode($info['other_algorithm']),true);
        $info['other_variable'] = json_decode(json_encode($info['other_variable']),true);
        return $this->view('admin/standard/info',[
            'info' => $info,
            'id' => $id,
            'classify_list' => \app\model\Classify::select(),
            'variable' => json_encode($info['variable'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
            'other_algorithm' => json_encode($info['other_algorithm'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
            'other_variable' => json_encode($info['other_variable'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
        ]);
    }
    public function save($id = 0){
        $data = input('post.data');
        $other_standard = array_column($data['other_algorithm'],'id');
        $other_standard = array_unique($other_standard);
        foreach ($other_standard AS $key => $value){
            $data['other_standard'][] = [
                'id' => $value,
                'name' => model::getStandardName($value)
            ];
        }
        if (empty($id)){
            model::create($data);
        }else{
            model::find($id)->save($data);
        }
        return $this->msg();
    }

    public function delete($id){
        model::find($id)->delete();
        return $this->msg();
    }
    //D8/2*D8/2*G8*pi*md/1000000-(E8-F8*2)/2*(E8-F8*2)/2*K8*pi*md/1000000+1/3*pi*(K8-G8-H8)*(I8*I8/4+I8*E8/4+E8*E8/4)/1000000*md+J8*J8/4*H8*pi*md/1000000-(E8-F8*2)*(E8-F8*2)/4*H8*pi*md/1000000-G5*G5/4*pi*md*G8/1000000*I5
    //(d8**2*g8-((E8-F8*2)/2)**2*k8)*pi*md/1000000+1/3*pi*(K8-G8-H8)*(I8*I8/4+I8*E8/4+E8*E8/4)/1000000*md
    
}